package ace.module.weixin.core.impl.dao.aggregate.impl;

import ace.module.weixin.core.impl.dao.aggregate.WxsCacheAggregate;
import ace.module.weixin.core.impl.dao.entity.QWxsCache;
import ace.module.weixin.core.impl.dao.repository.WxsCacheRepository;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 微信缓存数据,保存accessToken、ticket等有时效数据 aggregate implement.
 *
 * @author Caspar
 */
@Slf4j
@AllArgsConstructor
@Component
public class WxsCacheAggregateImpl implements WxsCacheAggregate {

  private final WxsCacheRepository wxsCacheRepository;

  @Transactional(rollbackFor = Exception.class)
  @Override
  public long deleteByKeyAndDeleteFlag(String key, Long deleteFlag) {
    QWxsCache qwxsCache = QWxsCache.wxsCache;
    Predicate predicate = ExpressionUtils.anyOf(qwxsCache.key.eq(key),
        qwxsCache.deleteFlag.eq(deleteFlag)
    );
    return wxsCacheRepository.deleteWhere(predicate);
  }
}